/**
 * 
 */
package com.wilmar.process.web.actions;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.wilmar.common.util.PageinationEntity;
import com.wilmar.process.entity.ProcessInformation;
import com.wilmar.process.entity.User;
import com.wilmar.process.exception.NoUserSessionException;
import com.wilmar.process.web.spi.IUserSessionService;

/**
 * 所有工作流子系统的Action的父类，抽象出了公有对象
 * 
 * @author MaKuBEX E-mail:makubexnet@gmail.com
 * @version 1.0.0 at Jul 31, 2009 9:33:20 AM
 * 
 */
public abstract class ProcessActionSupport extends ActionSupport implements
		ServletRequestAware, SessionAware, ServletResponseAware {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = -2964439033806915883L;

	/**
	 * 表示字符串"operate-success"的常量
	 */
	protected static final String ACTION_RESULT_OPERATE_SUCCESS = "operate-success";

	/**
	 * http请求
	 */
	protected HttpServletRequest request;

	/**
	 * http会话中保存的键值对
	 */
	protected Map<String, Object> session;

	/**
	 * http响应
	 */
	protected HttpServletResponse response;

	/**
	 * 流程相关信息
	 */
	protected ProcessInformation processInformation;

	/**
	 * 分页信息
	 */
	protected PageinationEntity pageination;

	/**
	 * 获取用户对象的服务对象
	 */
	private IUserSessionService userSessionService;

	/**
	 * 获取当前用户
	 * 
	 * @return 当前用户
	 * @throws NoUserSessionException
	 *             如果无法获取用户相关信息
	 */
	protected User getCurrentUser() throws NoUserSessionException {
		User currentUser = null;
		try {
			currentUser = this.userSessionService.getCurrentUserObject();
			if (currentUser == null) {
				currentUser = this.userSessionService
						.getCurrentUserObject(session);
			}
			if (currentUser == null) {
				currentUser = this.userSessionService
						.getCurrentUserObject(this.request.getSession());
			}
		} catch (NoUserSessionException e) {
			try {
				currentUser = this.userSessionService
						.getCurrentUserObject(session);
			} catch (NoUserSessionException e2) {
				currentUser = this.userSessionService
						.getCurrentUserObject(this.request.getSession());
			}
		}
		return currentUser;
	}

	/**
	 * @return the pageination
	 */
	public PageinationEntity getPageination() {
		return pageination;
	}

	/**
	 * @return the processInformation
	 */
	public ProcessInformation getProcessInformation() {
		return processInformation;
	}

	/**
	 * @return the userSessionService
	 */
	public IUserSessionService getUserSessionService() {
		return userSessionService;
	}

	/**
	 * @param pageination
	 *            the pageination to set
	 */
	public void setPageination(PageinationEntity pageination) {
		this.pageination = pageination;
	}

	/**
	 * @param processInformation
	 *            the processInformation to set
	 */
	public void setProcessInformation(ProcessInformation processInformation) {
		this.processInformation = processInformation;
	}

	/**
	 * 
	 * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
	 */
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	/**
	 * 
	 * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse
	 *      (javax.servlet.http.HttpServletResponse)
	 */
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}

	/**
	 * 
	 * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
	 */
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	/**
	 * @param userSessionService
	 *            the userSessionService to set
	 */
	public void setUserSessionService(IUserSessionService userSessionService) {
		this.userSessionService = userSessionService;
	}
}
